//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
//---------------------------------------------------------------------------
#include "Outils.h"
//---------------------------------------------------------------------------

//Inclusion de registry.hpp pour utiliser la base de registre
#include <registry.hpp>
//Inclusion de eic.h pour les variables globale
#include "eic.h"

//Définition des ressources WebHelp EIC //
//Touche F3
#define NOM_LABEL_SUR_FORM  "lRefWebHelp"
#define ADRESSE_IP "192.168.80.78"
#define PORT       "8080"

//Touche Ctrl+F3
#define PATH_KEY    "\\software\\eic\\WebHelp"
#define VALUE_REF   "KeyRefWebHelp"
#define VALUE_LOG   "KeyLogiciel"
#define VALUE_VER   "KeyVersion"

//Lancement de WebHelp
#define PATH_WEBHELP    "X:\\WebHelp\\"
#define WEBHELP_EXE     "WebHelp.exe"

//---------------------------------------------------------------------------
//      Lancement du site Internet
//---------------------------------------------------------------------------
PACKAGE_OUTILS void VerifierRefWebHelp ( AnsiString &asRefWebHelp )
{
    bool bTrouver = false;
    AnsiString asAsresse = EmptyStr;

    if(     (!_NetAide_Logiciel.IsEmpty())
         && (!_NetAide_Version.IsEmpty()) ) {

        if(!asRefWebHelp.IsEmpty() ) {
            bTrouver = true;
        } else {
            for(int i =0; i<Screen->ActiveForm->ComponentCount; i++) {
                TComponent * Fils = Screen->ActiveForm->Components[i];
                TLabel * lTemp = dynamic_cast<TLabel*>(Fils);
                if(lTemp) {
                    if( lTemp->Name == NOM_LABEL_SUR_FORM) {
                        if(!lTemp->Caption.IsEmpty()) {
                            asRefWebHelp = lTemp->Caption;
                            bTrouver = true;
                            break;
                        }
                    }
                }
            }
        }
        
        if(bTrouver) {
            asAsresse = (AnsiString)"http://"+ADRESSE_IP+":"+PORT+
                                    "/Default.aspx?Reference="+asRefWebHelp+
                                    "&Logiciel="+_NetAide_Logiciel+
                                    "&Version="+_NetAide_Version;
            ShellExecute(NULL,"Open",asAsresse.c_str(),NULL, NULL, SW_SHOWMAXIMIZED);
        }else {
            MessageDlg("Aucune aide juridique n'est définie dans ce contexte.", mtWarning, TMsgDlgButtons() << mbOK, 0);
        }
    }
}
//---------------------------------------------------------------------------
//      Acces au paramétrage de WebHelp
//---------------------------------------------------------------------------
PACKAGE_OUTILS void AccesWebHelp( AnsiString &asRefWebHelp )
{
    if(     (!_NetAide_Logiciel.IsEmpty())
        &&  (!_NetAide_Version.IsEmpty())
        &&  (!asRefWebHelp.IsEmpty())   ) {
        TRegistry * registre = new TRegistry();

        registre->RootKey = HKEY_LOCAL_MACHINE;
        if (registre->KeyExists(PATH_KEY)) {
            registre->OpenKey(PATH_KEY,false);
            registre->WriteString(VALUE_REF,  asRefWebHelp);
            registre->WriteString(VALUE_LOG, _NetAide_Logiciel);
            registre->WriteString(VALUE_VER, _NetAide_Version);

            //Lancement de WebHelp
            ShellExecute(NULL,"Open",WEBHELP_EXE,NULL,PATH_WEBHELP,SW_SHOWMAXIMIZED);
        }else {
            MessageDlg("Le logiciel WebHelp n'est pas installé sur ce poste.", mtWarning, TMsgDlgButtons() << mbOK, 0);
        }
        delete registre;
    }
}
//---------------------------------------------------------------------------
